package com.atguigu.gmall.common.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gsy
 * @date 2021年09月13日19:12
 * Description
 */
@Configuration
public class DelayedConfig {
    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    //交换机
    @Bean
    public Exchange delayedExchange() {
        Map<String, Object> arguments = new HashMap<>();
        //交换机  路由类型
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(exchange_delay, "x-delayed-message",
                true, false, arguments);//自定义交换机
    }

    //队列
    @Bean
    public Queue delayedQueue() {
        return QueueBuilder.durable(queue_delay_1).build();
    }

    //绑定
    @Bean
    public Binding bindingDelayedQueueToDelayedExchange(@Qualifier("delayedQueue") Queue queue,
                                                        @Qualifier("delayedExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(routing_delay).noargs();
    }
}
